package com.heima.skydemo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.heima.skydemo.domin.common.PageResult;
import com.heima.skydemo.domin.dto.DishQueryPage;
import com.heima.skydemo.entity.Dish;
import com.heima.skydemo.mapper.DishMapper;
import com.heima.skydemo.service.IDishService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 菜品 服务实现类
 * </p>
 *
 * @author author
 * @since 2025-04-15
 */
@Service
@Slf4j
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish> implements IDishService {

    public PageResult pageQueryone(DishQueryPage dishQueryPage) {
        log.info("分页查询菜品：{}",dishQueryPage);

        int page = dishQueryPage.getPage();
        int pageSize = dishQueryPage.getPageSize();
        PageHelper.startPage(page, pageSize);
        LambdaQueryWrapper<Dish> wrapper = new LambdaQueryWrapper<Dish>().like(dishQueryPage.getName() != null, Dish::getName, dishQueryPage.getName())
                .eq(dishQueryPage.getCategoryId() != null, Dish::getCategoryId, dishQueryPage.getCategoryId())
                .eq(dishQueryPage.getStatus() != null, Dish::getStatus, dishQueryPage.getStatus());
        Page<Dish> list = (Page<Dish>) list(wrapper);

        PageResult pageResult = new PageResult();
        pageResult.setTotal(list.getTotal());
        pageResult.setRecords(list.getResult());
        return pageResult;
    }
}
